package com.lichhao.system.web.controller;

import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.bind.annotation.ResponseBody;

import com.lichhao.common.util.QueryParam;
import com.lichhao.common.web.controller.BaseController;
import com.lichhao.system.model.OmAreaBean;
import com.lichhao.system.service.OmAreaService;

@Controller
public class OmAreaController extends BaseController {

	@Autowired
	@Qualifier("omAreaService")
	private OmAreaService omAreaService;

	/*
	 * @RequestMapping(value = "/omArea/query")
	 * 
	 * @ResponseBody public String query(HttpServletRequest request,
	 * HttpServletResponse response, Model model) throws IOException {
	 * 
	 * QueryParam queryParam = new QueryParam(request); String json =
	 * omAreaService.queryList(queryParam);
	 * 
	 * // json = new String(json.getBytes("GB2312"), "UTF-8");
	 * 
	 * response.setHeader("Pragma", "No-cache");
	 * response.setHeader("Cache-Control", "no-cache");
	 * response.setDateHeader("Expires", 0); //
	 * response.setContentType("text/html;charset=UTF-8");
	 * response.setContentType("application/json;charset=utf8"); //
	 * response.setContentType("text/json;charset=utf-8"); //
	 * response.getWriter().write(json);
	 * 
	 * return json; }
	 */
	// 该方法，写入ie时，中文乱码
	@RequestMapping(value = "/omArea/query")
	public void query(HttpServletRequest request, HttpServletResponse response,
			Model model) throws IOException {

		QueryParam queryParam = new QueryParam(request);
		String json = omAreaService.queryList(queryParam);

		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/json;charset=utf-8");
		response.getWriter().write(json);
	}

	@RequestMapping(value = "/omArea/excel")
	public String excel(HttpServletRequest request, Model model) {

		QueryParam queryParam = new QueryParam(request);

		Query query = new Query();
		if (!StringUtils.isEmpty(queryParam.getQuery())) {
			query.addCriteria(Criteria.where(queryParam.getQtype()).is(
					queryParam.getQuery()));
		}
		query.skip(queryParam.getSkip()).limit(queryParam.getLimit()).sort()
				.on(queryParam.getSortname(), queryParam.getSortorder());

		List<OmAreaBean> list = omAreaService.getOmAreaDao().getMongoTemplate()
				.find(query, OmAreaBean.class);

		// List<OmAreaBean> list =
		// omAreaService.getOmAreaDao().getMongoTemplate().findAll(OmAreaBean.class);
		model.addAttribute("omAreaList", list);
		return "omAreaExcel";

	}

	@RequestMapping(value = "/omArea/add")
	public ModelAndView add() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("om/omAreaEdit");
		OmAreaBean bean = new OmAreaBean();
		mav.addObject("bean", bean);
		mav.addObject("operation", "add");
		return mav;

	}

	@RequestMapping(value = "/omArea/save", method = { RequestMethod.POST })
	public void save(HttpServletResponse response, OmAreaBean bean,
			String operation) throws IOException {

		if ("add".equals(operation)) {
			omAreaService.addRecord(bean);
		}
		if ("edit".equals(operation)) {
			omAreaService.updateRecord(bean);
		}

		response.setContentType("text/json;charset=utf-8");
		response.getWriter().write(
				"{\"success\":true,\"operation\":\"" + operation + "\"}");
	}

	@RequestMapping(value = "/omArea/delete", method = { RequestMethod.POST })
	public void delete(HttpServletResponse response, Integer[] ids)
			throws IOException {

		MongoTemplate template = omAreaService.getOmAreaDao()
				.getMongoTemplate();
		Query q = new Query();
		List list = Arrays.asList(ids);
		q.addCriteria(where("areaId").in(list));
		template.remove(q, OmAreaBean.class);
		response.setContentType("text/json;charset=utf-8");
		response.getWriter().write("{\"success\":true}");
	}

	@RequestMapping(value = "/omArea/edit/{areaId}")
	public ModelAndView edit(@PathVariable("areaId") Integer areaId) {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("om/omAreaEdit");
		OmAreaBean bean = new OmAreaBean();
		bean.setAreaId(areaId);
		bean = omAreaService.findRecord(bean);
		mav.addObject("bean", bean);
		mav.addObject("operation", "edit");
		return mav;

	}

	@RequestMapping(value = "/omAreaList", method = { RequestMethod.GET })
	public String list() {
		return "om/omAreaList";
	}

	@RequestMapping(value = "/{page}.html")
	public String viewPage(@PathVariable("page") String page) {
		return page;
	}

//	@RequestMapping(value = "/upload")
//	public String upload(HttpServletRequest request,
//		    HttpServletResponse response) throws IllegalStateException,
//			IOException {
//
//		   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//		   CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest
//		     .getFile("uploadfile");// 表单中对应的文件名；
//
//		if (!file.isEmpty()) {
//			file.transferTo(new File("C:/test/" + file.getOriginalFilename()));
//			return "success";
//		} else {
//			return "fail";
//		}
//	}
	
	
}
